/*
 * Copyright (c) 2018 Hitevision
 */

package tw.com.hitevision.whiteboard.android.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.Settings;
import tw.com.hitevision.whiteboard.android.state.PageProperty;

public class PageMenuView extends RelativeLayout {
    private PageProperty.Color color;
    private PageProperty.Pattern pattern;
    private OnChangeListener onChangeListener;
    private OnImportClickListener onImportClickListener;

    private PageProperty.Color tempColor;
    private PageProperty.Pattern tempPattern;

    private List<View> colors;
    private List<View> patterns;
    private Settings settings;

    public PageMenuView(Context context) {
        super(context);
        init(context);
    }

    public PageMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PageMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        final Resources r = getResources();

        colors = new ArrayList<>();
        patterns = new ArrayList<>();

        setBackgroundResource(R.drawable.menu_bubble_page);

        final int paddingTop = r.getDimensionPixelSize(R.dimen.page_menu_padding_top);
        final int padding = r.getDimensionPixelSize(R.dimen.page_menu_padding);
        setPadding(padding, paddingTop, padding, 0);

        inflate(context, R.layout.menu_page, this);

        View color1 = findViewById(R.id.color_1);
        color1.setOnClickListener(colorsOnClickListener);
        View color2 = findViewById(R.id.color_2);
        color2.setOnClickListener(colorsOnClickListener);
        View color3 = findViewById(R.id.color_3);
        color3.setOnClickListener(colorsOnClickListener);
        View color4 = findViewById(R.id.color_4);
        color4.setOnClickListener(colorsOnClickListener);
        View color5 = findViewById(R.id.color_5);
        color5.setOnClickListener(colorsOnClickListener);
        View color6 = findViewById(R.id.color_6);
        if (color6 != null) {
            color6.setOnClickListener(colorsOnClickListener);
        }
        View color7 = findViewById(R.id.color_7);
        if (color7 != null) {
            color7.setOnClickListener(colorsOnClickListener);
        }

        colors.add(color1);
        colors.add(color2);
        colors.add(color3);
        colors.add(color4);
        colors.add(color5);
        if (color6 != null) {
            colors.add(color6);
        }
        if (color7 != null) {
            colors.add(color7);
        }

        View patternBlank = findViewById(R.id.pattern_blank);
        if (patternBlank != null) {
            patternBlank.setOnClickListener(patternsOnClickListener);
            patterns.add(patternBlank);
        }
        View patternLines = findViewById(R.id.pattern_lines);
        if (patternLines != null) {
            patternLines.setOnClickListener(patternsOnClickListener);
            patterns.add(patternLines);
        }
        View patternGrid = findViewById(R.id.pattern_grid);
        if (patternGrid != null) {
            patternGrid.setOnClickListener(patternsOnClickListener);
            patterns.add(patternGrid);
        }
        View patternNotebook = findViewById(R.id.pattern_notebook);
        if (patternNotebook != null) {
            patternNotebook.setOnClickListener(patternsOnClickListener);
            patterns.add(patternNotebook);
        }
        View patternAlphabet5Sets = findViewById(R.id.pattern_alphabet_5_sets);
        if (patternAlphabet5Sets != null) {
            patternAlphabet5Sets.setOnClickListener(patternsOnClickListener);
            patterns.add(patternAlphabet5Sets);
        }
        View patternAlphabet9Sets = findViewById(R.id.pattern_alphabet_9_sets);
        if (patternAlphabet9Sets != null) {
            patternAlphabet9Sets.setOnClickListener(patternsOnClickListener);
            patterns.add(patternAlphabet9Sets);
        }
        View patternChecklist = findViewById(R.id.pattern_checklist);
        if (patternChecklist != null) {
            patternChecklist.setOnClickListener(patternsOnClickListener);
            patterns.add(patternChecklist);
        }
        View patternSwot = findViewById(R.id.pattern_swot);
        if (patternSwot != null) {
            patternSwot.setOnClickListener(patternsOnClickListener);
            patterns.add(patternSwot);
        }
        View patternMusicTreble = findViewById(R.id.pattern_music_treble);
        if (patternMusicTreble != null) {
            patternMusicTreble.setOnClickListener(patternsOnClickListener);
            patterns.add(patternMusicTreble);
        }
        View patternMusicBass = findViewById(R.id.pattern_music_bass);
        if (patternMusicBass != null) {
            patternMusicBass.setOnClickListener(patternsOnClickListener);
            patterns.add(patternMusicBass);
        }
        View patternBaseball = findViewById(R.id.pattern_baseball);
        if (patternBaseball != null) {
            patternBaseball.setOnClickListener(patternsOnClickListener);
            patterns.add(patternBaseball);
        }
        View patternBasketball = findViewById(R.id.pattern_basketball);
        if (patternBasketball != null) {
            patternBasketball.setOnClickListener(patternsOnClickListener);
            patterns.add(patternBasketball);
        }
        View patternSoccer = findViewById(R.id.pattern_soccer);
        if (patternSoccer != null) {
            patternSoccer.setOnClickListener(patternsOnClickListener);
            patterns.add(patternSoccer);
        }
        View patternFootball = findViewById(R.id.pattern_football);
        if (patternFootball != null) {
            patternFootball.setOnClickListener(patternsOnClickListener);
            patterns.add(patternFootball);
        }
        View patternImport = findViewById(R.id.pattern_import);
        if (patternImport != null) {
            patternImport.setOnClickListener(importOnClickListener);
            patterns.add(patternImport);
        }

        View okay = findViewById(R.id.okay);
        okay.setOnClickListener(v -> {
            color = tempColor;
            pattern = tempPattern;

            if (onChangeListener != null) {
                onChangeListener.onPageOptionsChange(color, pattern);
            }
            setVisibility(View.GONE);
        });
        View cancel = findViewById(R.id.cancel);
        if (cancel != null) {
            cancel.setOnClickListener(v -> {
                resetSelections();
                setVisibility(View.GONE);
            });
        }
    }

    private final OnClickListener colorsOnClickListener = v -> {
        for (View view : colors) {
            view.setSelected(false);
        }
        v.setSelected(true);

        if (Props.getFlavorModel().equals(Props.Flavor.Model.Oem)) {
            switch (v.getId()) {
                case R.id.color_1:
                    tempColor = PageProperty.Color.GREEN;
                    break;
                case R.id.color_2:
                    tempColor = PageProperty.Color.YELLOW;
                    break;
                case R.id.color_3:
                    tempColor = PageProperty.Color.WHITE;
                    break;
                case R.id.color_4:
                    tempColor = PageProperty.Color.BLACK;
                    break;
                case R.id.color_5:
                    tempColor = PageProperty.Color.BLUE;
                    break;
            }
        } else {
            switch (v.getId()) {
                case R.id.color_1:
                    tempColor = PageProperty.Color.WHITE;
                    break;
                case R.id.color_2:
                    tempColor = PageProperty.Color.BLACK;
                    break;
                case R.id.color_3:
                    tempColor = PageProperty.Color.GREEN;
                    break;
                case R.id.color_4:
                    tempColor = PageProperty.Color.LIGHT_GREEN;
                    break;
                case R.id.color_5:
                    tempColor = PageProperty.Color.YELLOW;
                    break;
                case R.id.color_6:
                    tempColor = PageProperty.Color.LIGHT_YELLOW;
                    break;
                case R.id.color_7:
                    tempColor = PageProperty.Color.BLUE;
                    break;
            }
        }
    };

    private final OnClickListener patternsOnClickListener = v -> {
        for (View view : patterns) {
            view.setSelected(false);
        }
        v.setSelected(true);

        switch (v.getId()) {
            case R.id.pattern_blank:
                tempPattern = PageProperty.Pattern.BLANK;
                break;
            case R.id.pattern_lines:
                tempPattern = PageProperty.Pattern.LINES;
                break;
            case R.id.pattern_grid:
                tempPattern = PageProperty.Pattern.GRID;
                break;
            case R.id.pattern_notebook:
                tempPattern = PageProperty.Pattern.NOTEBOOK;
                break;
            case R.id.pattern_alphabet_5_sets:
                tempPattern = PageProperty.Pattern.ALPHABET_5_SET;
                break;
            case R.id.pattern_alphabet_9_sets:
                tempPattern = PageProperty.Pattern.ALPHABET_9_SET;
                break;
            case R.id.pattern_checklist:
                tempPattern = PageProperty.Pattern.CHECK_LIST;
                break;
            case R.id.pattern_swot:
                tempPattern = PageProperty.Pattern.SWOT;
                break;
            case R.id.pattern_music_treble:
                tempPattern = PageProperty.Pattern.MUSIC_TREBLE;
                break;
            case R.id.pattern_music_bass:
                tempPattern = PageProperty.Pattern.MUSIC_BASS;
                break;
            case R.id.pattern_baseball:
                tempPattern = PageProperty.Pattern.BASEBALL;
                break;
            case R.id.pattern_basketball:
                tempPattern = PageProperty.Pattern.BASKETBALL;
                break;
            case R.id.pattern_soccer:
                tempPattern = PageProperty.Pattern.SOCCER;
                break;
            case R.id.pattern_football:
                tempPattern = PageProperty.Pattern.FOOTBALL;
                break;
            case R.id.pattern_import:
                tempPattern = PageProperty.Pattern.IMPORT;
                break;
        }
    };

    private View.OnClickListener importOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            for (View view : patterns) {
                view.setSelected(false);
            }
            v.setSelected(true);
            tempPattern = PageProperty.Pattern.IMPORT;

            if (onImportClickListener != null) {
                onImportClickListener.onImportClick();
            }
        }
    };

    private void resetSelections() {
        if (color != tempColor) {
            setColor(color);
        }

        resetPatternSelection();
    }

    public void resetPatternSelection() {
        if (pattern != tempPattern) {
            setPattern(pattern);
        }
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);

        if (visibility != View.VISIBLE) {
            resetSelections();
        }
    }

    public void setColor(PageProperty.Color color) {
        if (color == null) {
            return;
        }

        this.color = color;
        tempColor = color;

        int viewId = 0;
        if (Props.getFlavorModel().equals(Props.Flavor.Model.Oem)) {
            switch (color) {
                case GREEN:
                    viewId = R.id.color_1;
                    break;
                case YELLOW:
                    viewId = R.id.color_2;
                    break;
                case WHITE:
                    viewId = R.id.color_3;
                    break;
                case BLACK:
                    viewId = R.id.color_4;
                    break;
                case BLUE:
                    viewId = R.id.color_5;
                    break;
            }
        } else {
            switch (color) {
                case WHITE:
                    viewId = R.id.color_1;
                    break;
                case BLACK:
                    viewId = R.id.color_2;
                    break;
                case GREEN:
                    viewId = R.id.color_3;
                    break;
                case LIGHT_GREEN:
                    viewId = R.id.color_4;
                    break;
                case YELLOW:
                    viewId = R.id.color_5;
                    break;
                case LIGHT_YELLOW:
                    viewId = R.id.color_6;
                    break;
                case BLUE:
                    viewId = R.id.color_7;
                    break;
            }
        }

        for (View view : colors) {
            view.setSelected(view.getId() == viewId);
        }
    }

    public void setPattern(PageProperty.Pattern pattern) {
        if (pattern == null) {
            return;
        }

        this.pattern = pattern;
        tempPattern = pattern;

        int viewId = 0;
        switch (pattern) {
            case BLANK:
                viewId = R.id.pattern_blank;
                break;
            case LINES:
                viewId = R.id.pattern_lines;
                break;
            case GRID:
                viewId = R.id.pattern_grid;
                break;
            case NOTEBOOK:
                viewId = R.id.pattern_notebook;
                break;
            case ALPHABET_5_SET:
                viewId = R.id.pattern_alphabet_5_sets;
                break;
            case ALPHABET_9_SET:
                viewId = R.id.pattern_alphabet_9_sets;
                break;
            case CHECK_LIST:
                viewId = R.id.pattern_checklist;
                break;
            case SWOT:
                viewId = R.id.pattern_swot;
                break;
            case MUSIC_TREBLE:
                viewId = R.id.pattern_music_treble;
                break;
            case MUSIC_BASS:
                viewId = R.id.pattern_music_bass;
                break;
            case BASEBALL:
                viewId = R.id.pattern_baseball;
                break;
            case BASKETBALL:
                viewId = R.id.pattern_basketball;
                break;
            case SOCCER:
                viewId = R.id.pattern_soccer;
                break;
            case FOOTBALL:
                viewId = R.id.pattern_football;
                break;
            case IMPORT:
                viewId = R.id.pattern_import;
                break;
        }

        for (View view : patterns) {
            view.setSelected(view.getId() == viewId);
        }
    }

    public void setOnChangeListener(OnChangeListener listener) {
        onChangeListener = listener;
    }

    public void clickOnOkay() {
        View okay = findViewById(R.id.okay);
        okay.callOnClick();
    }

    public void setOnImportClickListener(OnImportClickListener listener) {
        onImportClickListener = listener;
    }

    public interface OnChangeListener {
        void onPageOptionsChange(PageProperty.Color color, PageProperty.Pattern pattern);
    }

    public interface OnImportClickListener {
        void onImportClick();
    }
}
